package com.wy.component.communication;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.http.HttpServletRequest;

public class IpGetter {

	public static String getHtmlConentByUrl() {
		try {
			URL url = new URL("http://cloud.365ccyx.com/vy/getIP.php");
			HttpURLConnection con = (HttpURLConnection) url.openConnection();

			con.setInstanceFollowRedirects(false);
			con.setUseCaches(false);
			con.setAllowUserInteraction(false);
			con.connect();
			StringBuffer sb = new StringBuffer();
			String line = "";
			BufferedReader URLinput = new BufferedReader(new InputStreamReader(
					con.getInputStream()));
			while ((line = URLinput.readLine()) != null) {
				sb.append(line);
			}
			con.disconnect();

			String result = sb.toString().toLowerCase();
			result = result.substring(0,result.indexOf("<"));
			result = result.substring(result.lastIndexOf(" ") + 1);
			
			return result;
		} catch (Exception e) {

			return null;
		}
	}
	
	 public static String getIpAddr(HttpServletRequest request) {      
		String ip = null;
		if (ip == null || ip.length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0
				|| "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
       return ip;     
    }  
	 
}